package com.gao_de;


import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @ClassName: GaoDeRestfulAPI
 * @Author: ZhangYue26
 * @Description: 调用高德对外开放API TODO 参考： https://lbs.amap.com/api/webservice/guide/api/weatherinfo/#t1
 * @Date: 2020-08-14
 */
public class GaoDeRestfulAPI {
    public static void main(String[] args) {
//        获取天气(GaodeCityCodeTable.河西区.getAdcode());
//        IP定位("60.29.152.11");

    }




    public static void 获取天气(String cityCode) {
        URL url = null;
        HttpURLConnection httpurlconnection = null;
        try {
            url = new URL("https://restapi.amap.com/v3/weather/weatherInfo");
            //以GET方式请求
            httpurlconnection = (HttpURLConnection) url.openConnection();
            httpurlconnection.setDoOutput(true);
            httpurlconnection.setRequestMethod("GET");
            String variable = "key=4495baf04d89ed75b19e77a99e7afe7b&city="+cityCode;
            httpurlconnection.getOutputStream().write(variable.getBytes());
            httpurlconnection.getOutputStream().flush();
            httpurlconnection.getOutputStream().close();

            //获取响应代码
            int code = httpurlconnection.getResponseCode();
            System.out.println("code   " + code);

            //获取页面内容
            java.io.InputStream in = httpurlconnection.getInputStream();
            java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            String str = breader.readLine();
            while (str != null) {
                System.out.println(str);
                str = breader.readLine();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (httpurlconnection != null)
                httpurlconnection.disconnect();
        }
    }

    public static void IP定位(String ip) {
        URL url = null;
        HttpURLConnection httpurlconnection = null;
        try {
            url = new URL("https://restapi.amap.com/v3/ip");
            //以GET方式请求
            httpurlconnection = (HttpURLConnection) url.openConnection();
            httpurlconnection.setDoOutput(true);
            httpurlconnection.setRequestMethod("GET");
            String variable = "key=4495baf04d89ed75b19e77a99e7afe7b&ip="+ip;
            httpurlconnection.getOutputStream().write(variable.getBytes());
            httpurlconnection.getOutputStream().flush();
            httpurlconnection.getOutputStream().close();

            //获取响应代码
            int code = httpurlconnection.getResponseCode();
            System.out.println("code   " + code);

            //获取页面内容
            java.io.InputStream in = httpurlconnection.getInputStream();
            java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            String str = breader.readLine();
            while (str != null) {
                System.out.println(str);
                str = breader.readLine();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (httpurlconnection != null)
                httpurlconnection.disconnect();
        }
    }

}
